_ = require 'underscore'
ImportFiles = require '../../../helpers/import_files'
WebConfig = require './web_config'
WaterfallsDataHandler = require '../../../helpers/waterfalls_data_handler'
Utils = require '../../../helpers/utils'

viewContentHandler = 
  home : (req, view, title, navIndex, commodities, pageConfig, selectedTag) ->
    _.each commodities, (item) ->
      item.type = 'commodity'
      item.picUrl = item.pics?[0]?.url || ''
      item.cutDesc = Utils.cutStringByViewSize item.desc || item.title, 15 * 3

    waterfallsDataHandler = WaterfallsDataHandler.getWaterfallsDataHandler()

    itemFilter = 
      type : 'commodityFilter'
      tags : getComodityTags()
      selectedTag : selectedTag

      
    waterfallsDataHandler.add itemFilter, 0, 510
    waterfallsDataHandler.add commodities

    # console.dir waterfallsDataHandler.getConfig true
    pageButtonSetConfig = getButtonSetConfig pageConfig


    renderData = 
      title : title || ''
      layout : 'ys/layout'
      importObj : ImportFiles.getImportObj view
      header : WebConfig.getHeader navIndex || 0
      viewContent : 
        waterfallsConfig : waterfallsDataHandler.getConfig true
        pageButtonSetConfig : pageButtonSetConfig
    return renderData

  commodity : (req, view, title, navIndex, commodity) -> 
    commodity.picUrl = commodity.pics?[0]?.url || ''
    renderData = 
      title : title || ''
      layout : 'ys/layout'
      importObj : ImportFiles.getImportObj view
      header : WebConfig.getHeader navIndex || 0
      viewContent :
        commodity : commodity
    return renderData

  commodityModify : (req, view, title, navIndex, commodity) ->
    commodity.picUrl = commodity.pics?[0]?.url || ''
    console.dir commodity
    renderData = 
      title : title || ''
      layout : 'ys/layout'
      importObj : ImportFiles.getImportObj view
      header : WebConfig.getHeader navIndex || 0
      viewContent :
        commodity : commodity
        allTags : getComodityTags()
    return renderData

  table : (req, view, title, navIndex, tableData) ->
    renderData = 
      title : title : '',
      layout : 'ys/layout'
      importObj : ImportFiles.getImportObj view
      header : WebConfig.getHeader navIndex || 0
      viewContent : 
        tableData : tableData

    return renderData


getComodityTags = () ->
  brands = '箭牌 益达 劲浪 荷氏 维果C 曼妥思 阿尔卑斯 瑞士糖 百份百 趣满果 宝路 彩虹糖 雀巢 阿华田 桂格 立顿 元朗 金沙 好时 德芙 健逹 王老吉 波力 车仔 惠氏 美赞臣 多美滋 雅培 好立克 美禄 奥兰 能恩 子母 鹰唛 四洲 品客 百力滋 鹰牌 太古'.split ' '
  categories = '口香糖 润喉糖 木糖醇 糖果 奶粉 麦片 咖啡 奶茶 茶 薯片 巧克力 威化 米粉 饮料 方糖'.split ' '

  tags = []
  _.each brands, (brand) ->
    tags.push {
      name : brand
    }
  _.each categories, (category) ->
    tags.push {
      name : category
    }
  return tags

getButtonSetConfig = (pageConfig) ->
  start = pageConfig.currentPage || 1
  eachPageTotal = pageConfig.eachPageTotal || 1
  total = pageConfig.total || 1
  end = Math.ceil total / eachPageTotal
  if start is end
    return null
  pageButtonSetConfig =
    start : start
    end : end
    total : 10
  return pageButtonSetConfig

module.exports = viewContentHandler






# waterfallData = []
# waterfallData.push itemFilter, itemData, itemData, itemData, itemData, itemData
# testData.push waterfallData, waterfallData, waterfallData, waterfallData